package com.example.chatchatbackend.service.impl.sendJob;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.send.SendAdv;
import com.example.chatchatbackend.entity.vo.response.SendAdvVO;
import com.example.chatchatbackend.mapper.SendAdvMapper;
import com.example.chatchatbackend.service.sendJob.SendAdvService;
import com.example.chatchatbackend.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@Service
public class SendAdvServiceImpl extends ServiceImpl<SendAdvMapper, SendAdv> implements SendAdvService {

    private static final LocalDateTime currentTime = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private SendAdvMapper sendAdvMapper;



    @Override
    public List<SendAdvVO> getAll() {
        List<SendAdv> sendAdvList = sendAdvMapper.selectAll();
        List<SendAdvVO> sendAdvVOList = new ArrayList<>();
        for(SendAdv sendAdv : sendAdvList){
            sendAdvVOList.add(new SendAdvVO(sendAdv.getId(),sendAdv.getTitle(),sendAdv.getContent(),sendAdv.getCreatedTime(),sendAdv.getImageUrl(),sendAdv.getSendTime(),sendAdv.getUpdateTime(),sendAdv.getEndTime(),sendAdv.getIsSend(),sendAdv.getCreatorId(),sendAdv.getCreatorName()));
        }
        return sendAdvVOList;
    }

    @Override
    public SendAdvVO insertSendAdv(SendAdvVO sendAdvVO) {
        SendAdv sendAdv = new SendAdv();
        BeanUtils.copyProperties(sendAdvVO, sendAdv);
        if(this.save(sendAdv))
            return sendAdvVO;
        return null;
    }

    @Override
    public List<SendAdvVO> insertSendAdvList(List<SendAdvVO> sendAdvVOList) {

        List<SendAdv> sendAdvList = new ArrayList<>();

        for (SendAdvVO sendAdvVO : sendAdvVOList) {
            sendAdvList.add(new SendAdv(null,sendAdvVO.getTitle(),sendAdvVO.getContent(),this.currentTime,sendAdvVO.getImageUrl(),sendAdvVO.getSendTime(),this.currentTime,sendAdvVO.getEndTime(),"0",sendAdvVO.getCreatorId()));
        }
        sendAdvMapper.insertList(sendAdvList);
        log.info("{}",sendAdvList);
        return sendAdvVOList;
    }


    @Override
    public IPage<SendAdv> pageThree(Integer pageNum, Integer pageSize) {
        Page<SendAdv> page = new Page<>(pageNum, pageSize);
        IPage<SendAdv> sendAdvIPage = sendAdvMapper.selectPage(page, null);
        return sendAdvIPage;
    }

    @Override
    public SendAdvVO updateSendAdv(SendAdvVO sendAdvVO) {
        SendAdv sendAdv = new SendAdv();
        BeanUtils.copyProperties(sendAdvVO, sendAdv);
        sendAdv.setUpdateTime(this.currentTime);
        sendAdvMapper.updateSendAdv(sendAdv);
        return sendAdvVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        sendAdvMapper.deleteBatchIds(ids);
    }

    @Override
    public List<SendAdvVO> findByIds(List<Integer> ids) {
        List<SendAdv> sendAdvList = sendAdvMapper.selectBatchIds(ids);
        List<SendAdvVO> sendAdvVOList = new ArrayList<>();

        for (SendAdv sendAdv : sendAdvList) {
            SendAdvVO sendAdvVO = new SendAdvVO();
            BeanUtils.copyProperties(sendAdv, sendAdvVO);
            sendAdvVOList.add(sendAdvVO);  // 添加到结果列表
        }

        return sendAdvVOList;
    }

    @Override
    public List<SendAdvVO> findByTitle(String title) {

        LambdaQueryWrapper<SendAdv> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), SendAdv::getTitle, title);
        List<SendAdv> sendAdvList = sendAdvMapper.selectList(wrapper);

        List<SendAdvVO> sendAdvVOList = new ArrayList<>();

        for (SendAdv sendAdv : sendAdvList) {
            SendAdvVO sendAdvVO = new SendAdvVO();
            BeanUtils.copyProperties(sendAdv, sendAdvVO);
            sendAdvVOList.add(sendAdvVO);  // 添加到结果列表
        }
        return sendAdvVOList;
    }

    @Override
    public List<SendAdvVO> findTasksToSend(LocalDateTime currentTime) {


        List<SendAdv> sendStudents = sendAdvMapper.findSendAdvList("0",currentTime);

        List<SendAdvVO> sendStudentVos = sendStudents.stream()
                .map(sendStudent -> {
                    SendAdvVO sendStudentVo = new SendAdvVO();
                    BeanUtils.copyProperties(sendStudent, sendStudentVo);
                    return sendStudentVo;
                })
                .collect(Collectors.toList()); // 使用 collect(Collectors.toList())

        return sendStudentVos;
    }


    @Override
    public void updateIsSend(SendAdvVO sendAdvVO) {
        SendAdv sendAdv = new SendAdv();
        sendAdv.setIsSend("1");
        sendAdv.setUpdateTime(this.currentTime);
        sendAdv.setId(sendAdvVO.getId()); // 设置要更新的记录 ID
        // 执行更新操作
        sendAdvMapper.updateById(sendAdv);
    }


}
